package com.mall.service.impl;

import com.mall.common.utils.Security;
import com.mall.controller.vo.GoodsCategoryVO;
import com.mall.dao.custom.CtFavoriteMapper;
import com.mall.dao.custom.CtGoodsMapper;
import com.mall.entity.Favorite;
import com.mall.entity.Goods;
import com.mall.service.GoodsService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xyl
 * @date 2021-02-12 17:19
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private CtGoodsMapper goodsMapper;
    @Autowired
    private CtFavoriteMapper favoriteMapper;
    @Override
    public int add(Goods goods) {
        goods.setGoodsStatus(1); //默认为正常
        goods.setLowPrice(goods.getPrice()); //秒杀价默认为正常价格
        goods.setSaleNumber(0);
        goods.setCreateBy(Security.getCurrentUserId());
        goods.setCreateTime(new Date());
        if(ObjectUtils.isNotEmpty(goods.getLabelList())){
            String lable = String.join(",", goods.getLabelList());
            goods.setLabel(lable);
        }
        return goodsMapper.insert(goods);
    }

    @Override
    public List<Goods> search(String searchKey,String categoryName) {
        List<Goods> goods = goodsMapper.selectBySearchKeyAndCategoryName(searchKey,categoryName);
        for (Goods gds : goods) {
            gds.setLabelList(labelToList(gds.getLabel()));
        }
        return goods;
    }

    @Override
    public int modifyGoodsById(Goods goods) {
        goods.setUpdateTime(new Date());
        goods.setUpdateBy(Security.getCurrentUserId());
        if(ObjectUtils.isNotEmpty(goods.getLabelList())){
            String lable = String.join(",", goods.getLabelList());
            goods.setLabel(lable);
        }
        return goodsMapper.updateByPrimaryKeySelective(goods);
    }

    @Override
    public int removeGoodsById(Integer goodsId) {
        return goodsMapper.deleteByPrimaryKey(goodsId);
    }


    @Override
    public List<Goods> getRandomGoods(Goods goods) {
        List<Goods> goods1 = goodsMapper.selectRandomList(goods);
        for (Goods gds : goods1) {
            gds.setLabelList(labelToList(gds.getLabel()));
        }
        return goods1;
    }

    @Override
    public List<Goods> getGoodsList(Goods goods) {
        List<Goods> goods1 = goodsMapper.selectList(goods);
        for (Goods gds : goods1) {
            gds.setLabelList(labelToList(gds.getLabel()));
        }
        return goods1;
    }

    @Override
    public List<GoodsCategoryVO> getGooodsByCategory() {
        List<GoodsCategoryVO> list = new LinkedList<>();
        List<String> cts =  goodsMapper.selectAllCategoryName();
        for (int i = 0; i < cts.size(); i++) {
            String categoryName = cts.get(i);
            GoodsCategoryVO ccvo = new GoodsCategoryVO();
            List<Goods> gcvos =  this.getGoodsByCategoryName(categoryName);
            for (Goods gcvo : gcvos) {
                gcvo.setLabelList(labelToList(gcvo.getLabel()));
            }
            ccvo.setGoodsList(gcvos);
            ccvo.setTitle(categoryName);
            ccvo.setId(i);
            list.add(ccvo);
        }
        return list;
    }

    @Override
    public List<Goods> getGoodsByCategoryName(String categoryName) {
        List<Goods> goods = goodsMapper.selectByCategoryNames(categoryName);
        for (Goods good : goods) {
            good.setLabelList(labelToList(good.getLabel()));
        }
        return goods;
    }

    @Override
    public Goods getGoodsById(Integer goodsId) {
        Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
        goods.setLabelList(labelToList(goods.getLabel()));
        //todo 判断商品是否被收藏
        Favorite favorite = favoriteMapper.selectFavoriteByGoodsIdAndUserId(goodsId,Security.getCurrentUserId());
        if (ObjectUtils.isNotEmpty(favorite)) {
            goods.setFavorite(true);
        }
        return goods;
    }

    @Override
    public List<Goods> getGoodsByFavorite() {
        List<Favorite> favorites = favoriteMapper.selectFavoriteByUserId(Security.getCurrentUserId());
        List<Integer> collect = favorites.stream().map(Favorite::getGoodsId).collect(Collectors.toList());
        List<Goods> goods = goodsMapper.selectByGoodsIds(collect);
        for (Goods good : goods) {
            good.setLabelList(labelToList(good.getLabel()));
        }
        return goods;
    }

    @Override
    public List<Goods> listAll() {
        List<Goods> goods = goodsMapper.selectAll();
        for (Goods good : goods) {
            good.setLabelList(labelToList(good.getLabel()));
        }
        return goods;
    }

    @Override
    public List<Goods> listBySeq(String categoryName, Integer seq) {
        List<Goods> goods = goodsMapper.selectBySeq(categoryName,seq);
        for (Goods good : goods) {
            good.setLabelList(labelToList(good.getLabel()));
        }
        return goods;
    }

    private List<String> labelToList(String label){
        if(StringUtils.isNotEmpty(label)){
            return Arrays.asList(label.split(","));
        }
        return null;
    }

}
